<template>
  <el-drawer size="80%" :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="快递流水号" prop="expressSerial">
        <el-input v-model="formData.expressSerial" placeholder="保存后自动生成"  disabled />
      </el-form-item>
      <el-form-item label="运单号" prop="trackingNumber">
        <el-input v-model="formData.trackingNumber" placeholder="请输入运单号" />
      </el-form-item>
      <el-form-item label="寄件企业" prop="senderEnterprise">
        <el-input v-model="formData.senderEnterprise" placeholder="请输入寄件企业" />
      </el-form-item>
      <el-form-item label="收件人" prop="recipient">
        <el-input v-model="formData.recipient" placeholder="请输入收件人" />
      </el-form-item>
      <el-form-item label="收件企业" prop="receiverEnterprises">
        <el-input v-model="formData.receiverEnterprises" placeholder="请输入收件企业" />
      </el-form-item>
      <el-form-item label="价格" prop="price">
        <el-input v-model="formData.price" placeholder="请输入价格" />
      </el-form-item>
      <el-form-item label="寄件日期" prop="sendDate">
        <el-date-picker
          v-model="formData.sendDate"
          type="date"
          value-format="x"
          placeholder="选择寄件日期"
        />
      </el-form-item>
      <el-form-item label="是否已收款" prop="isPaid">
        <el-radio-group v-model="formData.isPaid">
          <el-radio
            v-for="dict in getIntDictOptions(DICT_TYPE.OA_PAY_TYPE)"
            :key="dict.value"
            :label="dict.value"
            :value="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="快递内容" prop="content">
        <el-input
          v-model="formData.content"
          style="width: 100%;"
          :rows="4"
          type="textarea"
          placeholder="请输入快递内容"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </el-drawer>
</template>
<script setup lang="ts">
import {  DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import { ExpressManagementApi, ExpressManagementVO } from '@/api/oa/expressmanagement'

/** 快递管理 表单 */
defineOptions({ name: 'ExpressManagementForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('')
const formData = ref({
  id: undefined,
  expressSerial: undefined,
  senderEnterprise: undefined,
  receiverEnterprises: undefined,
  price: undefined,
  content: undefined,
  trackingNumber: undefined,
  recipient: undefined,
  sendDate: undefined,
  isPaid: 0
})
const formRules = reactive({
  senderEnterprise: [{ required: true, message: '寄件企业不能为空', trigger: 'blur' }],
  receiverEnterprises: [{ required: true, message: '收件企业不能为空', trigger: 'blur' }],
  trackingNumber: [{ required: true, message: '运单号不能为空', trigger: 'blur' }],
  recipient: [{ required: true, message: '收件人不能为空', trigger: 'blur' }],
  sendDate: [{ required: true, message: '寄件日期不能为空', trigger: 'blur' }],
  price: [{ required: true, message: '价格不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref


/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await ExpressManagementApi.getExpressManagement(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as ExpressManagementVO
    if (formType.value === 'create') {
      await ExpressManagementApi.createExpressManagement(data)
      message.success(t('common.createSuccess'))
    } else {
      await ExpressManagementApi.updateExpressManagement(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    expressSerial: undefined,
    senderEnterprise: undefined,
    receiverEnterprises: undefined,
    price: undefined,
    content: undefined,
    trackingNumber: undefined,
    recipient: undefined,
    sendDate: undefined,
    isPaid: 0
  }
  formRef.value?.resetFields()
}
</script>
